package com.naizhuai.Model.db.ainote;

import android.support.annotation.NonNull;

import java.util.List;
import java.util.Map;


import static com.naizhuai.util.AutoClearedValue.checkNotNull;

/**
 * @author：lichenxi
 * @date 2018/2/22 11
 * email：525603977@qq.com
 * Fighting
 */
public class NoteRepository implements NoteDataSourse {
    private static NoteRepository INSTANCE = null;

    private NoteDataSourse mLocalNoteDataSourse;
    private NoteDataSourse mRemoteNoteDataSourse;

    Map<String, Note> mCachedTasks;

    private NoteRepository(NoteDataSourse localNoteDataSourse,
                           NoteDataSourse remoteNoteDataSourse) {
        mLocalNoteDataSourse = checkNotNull(localNoteDataSourse);
//        mRemoteNoteDataSourse=checkNotNull(remoteNoteDataSourse);
    }

    public static NoteRepository getInstance(NoteDataSourse localNoteDataSourse,
                                             NoteDataSourse remoteNoteDataSourse ) {
        if (INSTANCE == null) {
            INSTANCE = new NoteRepository(localNoteDataSourse,remoteNoteDataSourse);
        }
        return INSTANCE;
    }
    @Override
    public void getNotes(@NonNull LoadNotesCallback callback) {
        checkNotNull(callback);
        mLocalNoteDataSourse.getNotes(new LoadNotesCallback() {
            @Override
            public void onNotesLoaded(List<Note> notes) {
                 callback.onNotesLoaded(notes);
            }

            @Override
            public void onDataNotAvailable() {
                callback.onDataNotAvailable();
//                    mRemoteNoteDataSourse.getNotes(new LoadNotesCallback() {
//                        @Override
//                        public void onNotesLoaded(List<Note> notes) {
//                            callback.onNotesLoaded(notes);
//                        }
//
//                        @Override
//                        public void onDataNotAvailable() {
//                            callback.onDataNotAvailable();
//                        }
//                    });
                }


        });
    }

    @Override
    public void getNote(@NonNull int noteId, @NonNull GetNoteCallback callback) {
        checkNotNull(callback);
        //cache
        mLocalNoteDataSourse.getNote(noteId, new GetNoteCallback() {
            @Override
            public void onNoteLoaded(Note note) {
                callback.onNoteLoaded(note);
            }

            @Override
            public void onDataNotAvailable() {

//               mRemoteNoteDataSourse.getNote(noteId, new GetNoteCallback() {
//                   @Override
//                   public void onNoteLoaded(Note note) {
//                       callback.onNoteLoaded(note);
//                   }
//
//                   @Override
//                   public void onDataNotAvailable() {
//                       callback.onDataNotAvailable();
//                   }
//               });
            }
        });
    }

    @Override
    public void getTitleNotes(@NonNull LoadNotesCallback callback) {
        checkNotNull(callback);
        mLocalNoteDataSourse.getTitleNotes(new LoadNotesCallback() {
            @Override
            public void onNotesLoaded(List<Note> notes) {
                callback.onNotesLoaded(notes);
            }

            @Override
            public void onDataNotAvailable() {
                callback.onDataNotAvailable();
//                    mRemoteNoteDataSourse.getNotes(new LoadNotesCallback() {
//                        @Override
//                        public void onNotesLoaded(List<Note> notes) {
//                            callback.onNotesLoaded(notes);
//                        }
//
//                        @Override
//                        public void onDataNotAvailable() {
//                            callback.onDataNotAvailable();
//                        }
//                    });
            }


        });
    }

    @Override
    public void getCreationNotes(@NonNull LoadNotesCallback callback) {
        checkNotNull(callback);
        mLocalNoteDataSourse.getCreationNotes(new LoadNotesCallback() {
            @Override
            public void onNotesLoaded(List<Note> notes) {
                callback.onNotesLoaded(notes);
            }

            @Override
            public void onDataNotAvailable() {
                callback.onDataNotAvailable();

            }
        });
    }

    @Override
     public void saveNote(@NonNull Note note) {
         mLocalNoteDataSourse.saveNote(note);
     }

    @Override
    public void updateNote(@NonNull Note note) {
        mLocalNoteDataSourse.updateNote(note);
    }

    @Override
    public void deleteNotes(List<Note> notes) {
        mLocalNoteDataSourse.deleteNotes(notes);
    }


}
